package com.qj.daibei.controller;

import com.github.pagehelper.PageInfo;
import com.qj.daibei.pojo.Loan;
import com.qj.daibei.pojo.ShouldPayBack;
import com.qj.daibei.pojo.User;
import com.qj.daibei.service.LoanService;
import com.qj.daibei.service.ShouldPayBackService;
import com.qj.daibei.service.UserService;
import com.qj.daibei.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * author wml
 * Created on 2021/10/6 15:34
 */
@Controller
public class LoanController {

    @Autowired
    private LoanService loanService;

    @Autowired
    private ShouldPayBackService shouldPayBackService;

    @Autowired
    private UserService userService;

    @RequestMapping("/insertLoan")
    @ResponseBody
    public String insertLoan(Loan loan, @RequestPart(value = "file", required = false) MultipartFile multipartFile, HttpSession session) throws IOException {
//        String str = FileUpload.doSingleFileUpload(multipartFile, "test");
        User user = (User) session.getAttribute("loginUser");
        loan.setLendingTime(new Date());
        loan.setStartTime(new Date());
        loan.setMoney(loan.getMoneyTemp());
        loan.setUserId(user.getUserId());
//        System.out.println(loan);
        this.loanService.insertLoan(loan);
//        System.out.println(loan);

        // 往待还款表中插入数据-----start
        loan.setMoney(loan.getMoneyTemp()); // 模拟放款成功
        int month = loan.getTerm(); // 获取贷款期限
        float interest = 0.0f; // 每月利息
        if (month <= 12) {
            interest = loan.getMoney() * 0.0435f;
        } else if (month <= 60) {
            interest = loan.getMoney() * 0.0475f;
        } else {
            interest = loan.getMoney() * 0.049f;
        }
        float money = loan.getMoney() / (float) month + interest; // 每月应还钱数
        Date newDate = new Date(); // 模拟放款时间
        loan.setLendingTime(newDate); // 模拟放款时间
        Date date = loan.getLendingTime(); // 获取当前时间
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        List<ShouldPayBack> list = new ArrayList<>(); // 用来存放待还款记录
        for (int i = 0; i < loan.getTerm(); i++) {
            ShouldPayBack shouldPayBack = new ShouldPayBack();
            rightNow.add(Calendar.MONTH, 1);
            shouldPayBack.setSpbTime(rightNow.getTime());
            shouldPayBack.setSpbMoney(money);
            shouldPayBack.setLoanId(loan.getLoanId());
            shouldPayBack.setSpbStatus("未还款");
            list.add(shouldPayBack);
        }
        int result = this.shouldPayBackService.insertShouldPayBackByBatch(list);
        System.out.println(result);
        // 往待还款表中插入数据-----start

        // 往user表加零钱
        user.setPinMoney(user.getPinMoney() + loan.getMoney());
        this.userService.updatePinMoneyByUserId(user);

        return "ok";
    }

    @RequestMapping("/test1")
    @ResponseBody
//    public List<Loan> test1(HttpSession session, int pageNum, int pageSize) {
    public List<Loan> test1(HttpSession session) {
        int pageNum = 1, pageSize = 3;
        User user = (User) session.getAttribute("loginUser");
        List<Loan> loanList = this.loanService.findLoanByUserIdByPageFirst(user.getUserId(), pageNum, pageSize);
        return loanList;
    }

    @RequestMapping("/test2")
    @ResponseBody
//    public PageInfo<Loan> test2(HttpSession session, int pageNum, int pageSize) {
    public PageInfo<Loan> test2(HttpSession session) {
        int pageNum = 1, pageSize = 3;
        User user = (User) session.getAttribute("loginUser");
        PageInfo<Loan> pageInfo = this.loanService.findLoanByUserIdByPageSecond(user.getUserId(), pageNum, pageSize);
        return pageInfo;
    }
}
